package com.jackiepenghe.rangedatepickerlibrary

import android.util.Log

object Logger {

    private const val MAX_LENGTH = 2000

    /**
     * 关闭日志打印
     */
    @Suppress("MemberVisibilityCanBePrivate")
    const val LEVEL_OFF_LOG = 0

    /**
     * 日志打印为info级别
     */
    @Suppress("MemberVisibilityCanBePrivate")
    const val LEVEL_INFO = 1

    /**
     *
     */
    @Suppress("MemberVisibilityCanBePrivate")
    const val LEVEL_ASSERT = 2

    @Suppress("MemberVisibilityCanBePrivate")
    const val LEVEL_DEBUG = 3

    @Suppress("MemberVisibilityCanBePrivate")
    const val LEVEL_ERROR = 4

    @Suppress("MemberVisibilityCanBePrivate")
    const val LEVEL_VERBOSE = 5

    @Suppress("MemberVisibilityCanBePrivate")
    const val LEVEL_WARN = 6

    private var logLevel = LEVEL_OFF_LOG

    internal fun log(message: String) {
        val length: Int = message.length
        val count: Int = if (length % MAX_LENGTH != 0) {
            length / MAX_LENGTH + 1
        } else {
            length / MAX_LENGTH
        }
        when (logLevel) {
            LEVEL_INFO -> {
                logInfo(length, message, count)
            }

            LEVEL_ASSERT -> {
                logAssert(length, message, count)
            }

            LEVEL_DEBUG -> {
                logDebug(length, message, count)
            }

            LEVEL_ERROR -> {
                logError(length, message, count)
            }

            LEVEL_VERBOSE -> {
                logVerbose(length, message, count)
            }

            LEVEL_WARN -> {
                logWarn(length, message, count)
            }

            LEVEL_OFF_LOG -> {

            }

            else -> {

            }
        }
    }

    private fun logWarn(length: Int, message: String, count: Int) {
        if (length <= MAX_LENGTH) {
            Log.w("RangeDatePickerView", message)
            return
        }

        for (i in 0 until count) {
            if ((i + 1) * MAX_LENGTH > length) {
                Log.w(
                    "RangeDatePickerView",
                    "->" + message.substring(
                        i * MAX_LENGTH,
                        length
                    )
                )
            } else {
                val str: String = message.substring(
                    i * MAX_LENGTH,
                    (i + 1) * MAX_LENGTH
                )
                if (i == 0) {
                    Log.w("RangeDatePickerView", "$str->")
                    continue
                }
                Log.w("RangeDatePickerView", "->$str->")
            }
        }
    }

    private fun logVerbose(length: Int, message: String, count: Int) {
        if (length <= MAX_LENGTH) {
            Log.v("RangeDatePickerView", message)
            return
        }

        for (i in 0 until count) {
            if ((i + 1) * MAX_LENGTH > length) {
                Log.v(
                    "RangeDatePickerView",
                    "->" + message.substring(
                        i * MAX_LENGTH,
                        length
                    )
                )
            } else {
                val str: String = message.substring(
                    i * MAX_LENGTH,
                    (i + 1) * MAX_LENGTH
                )
                if (i == 0) {
                    Log.v("RangeDatePickerView", "$str->")
                    continue
                }
                Log.v("RangeDatePickerView", "->$str->")
            }
        }
    }

    private fun logError(length: Int, message: String, count: Int) {
        if (length <= MAX_LENGTH) {
            Log.e("RangeDatePickerView", message)
            return
        }

        for (i in 0 until count) {
            if ((i + 1) * MAX_LENGTH > length) {
                Log.e(
                    "RangeDatePickerView",
                    "->" + message.substring(
                        i * MAX_LENGTH,
                        length
                    )
                )
            } else {
                val str: String = message.substring(
                    i * MAX_LENGTH,
                    (i + 1) * MAX_LENGTH
                )
                if (i == 0) {
                    Log.e("RangeDatePickerView", "$str->")
                    continue
                }
                Log.e("RangeDatePickerView", "->$str->")
            }
        }
    }

    private fun logDebug(length: Int, message: String, count: Int) {
        if (length <= MAX_LENGTH) {
            Log.d("RangeDatePickerView", message)
            return
        }

        for (i in 0 until count) {
            if ((i + 1) * MAX_LENGTH > length) {
                Log.d(
                    "RangeDatePickerView",
                    "->" + message.substring(
                        i * MAX_LENGTH,
                        length
                    )
                )
            } else {
                val str: String = message.substring(
                    i * MAX_LENGTH,
                    (i + 1) * MAX_LENGTH
                )
                if (i == 0) {
                    Log.d("RangeDatePickerView", "$str->")
                    continue
                }
                Log.d("RangeDatePickerView", "->$str->")
            }
        }
    }

    private fun logAssert(length: Int, message: String, count: Int) {
        if (length <= MAX_LENGTH) {
            Log.wtf("RangeDatePickerView", message)
            return
        }

        for (i in 0 until count) {
            if ((i + 1) * MAX_LENGTH > length) {
                Log.wtf(
                    "RangeDatePickerView",
                    "->" + message.substring(
                        i * MAX_LENGTH,
                        length
                    )
                )
            } else {
                val str: String = message.substring(
                    i * MAX_LENGTH,
                    (i + 1) * MAX_LENGTH
                )
                if (i == 0) {
                    Log.wtf("RangeDatePickerView", "$str->")
                    continue
                }
                Log.wtf("RangeDatePickerView", "->$str->")
            }
        }
    }

    private fun logInfo(length: Int, message: String, count: Int) {
        if (length <= MAX_LENGTH) {
            Log.i("RangeDatePickerView", message)
            return
        }

        for (i in 0 until count) {
            if ((i + 1) * MAX_LENGTH > length) {
                Log.i(
                    "RangeDatePickerView",
                    "->" + message.substring(
                        i * MAX_LENGTH,
                        length
                    )
                )
            } else {
                val str: String = message.substring(
                    i * MAX_LENGTH,
                    (i + 1) * MAX_LENGTH
                )
                if (i == 0) {
                    Log.i("RangeDatePickerView", "$str->")
                    continue
                }
                Log.i("RangeDatePickerView", "->$str->")
            }
        }
    }

    fun setLogLevel(logLevel: Int) {
        this.logLevel = logLevel
    }

}